﻿namespace ShikijuHome.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using ShikijuHome.Models;
    using ShikijuHome.ViewModels;
    using System.Dynamic;

    // Temporary removed for easy testin
    // [Authorize(Roles = "Administrator, User")] 
    public class BlogController : Controller
    {
        public ActionResult Index()
        {
            var itemTable = new Posts();
            var posts = itemTable.All();
            return View(posts);
        }

        public ActionResult Details(int id)
        {
            var itemTable = new Posts();
            var post = itemTable.Single(where: "id = @0", args: id);
            var replyTable = new Reply();
            var replies = replyTable.Query(@"SELECT Reply.id, Reply.Message, Reply.ItemId, Reply.OwnerId, Users.AvatarUrl FROM Users INNER JOIN Reply ON Users.id = Reply.OwnerId WHERE Reply.ItemId = @0", id);
            var replyDictionary = new Dictionary<int,ReplyModel>();

            var blogViewModel = new BlogViewModel();
            foreach (var reply in replies)
            {
                replyDictionary.Add(reply.id, new ReplyModel(reply.id, reply.OwnerId, reply.ItemId, reply.Message, reply.AvatarUrl));
            }

            blogViewModel.Post = 
                new Tuple<int, string, Dictionary<int, ReplyModel>>(
                    post.id,
                    post.Message,
                    replyDictionary);
            
            return View(blogViewModel);
        }

        [HttpGet]
        public ActionResult Create()
        {
            var table = new Category();
            var categories = table.All();
            var categorylist = new List<SelectListItem>();
            var categoriescoll = categories.Select(x => x).ToList();
            foreach (var t in categoriescoll)
            {
                var item = new SelectListItem();
                item.Text = t.Name;
                item.Value = t.id.ToString();
                categorylist.Add(item);
            }
            ViewBag.Categoryid = categorylist;
            return View();
        } 

        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            try
            {
                var table = new Posts();
                dynamic post = new ExpandoObject();
                post.Message = form["Message"];
                post.Categoryid = int.Parse(form["Categoryid"]);
                table.Insert(post);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        public ActionResult Edit(int id)
        {
            var posts = new Posts();
            var post = posts.Single(where: "id  = @0", args: id);
            return View(post);
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult Reply(FormCollection form)
        {
            try
            {
                var table = new Reply();
                dynamic reply = new ExpandoObject();
                reply.Itemid = form["id"];
                reply.Message = form["replyTextArea"];
                var session = (ShikijuSessionObject) HttpContext.Session["ShikijuSessionObject"]; // checking if session is null shouldnt be needed tbh, authorize filter will handle this?
                var userReplying = session.id != 0 ? session.id : 1; // default is 1 (this should never happen ;))
                reply.Ownerid = userReplying;
                table.Insert(reply);
                return RedirectToAction("Details/" + form["id"]);
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        // AJAX: /Blog/DeleteReply/5
        [HttpPost]
        public ActionResult DeleteReply(int id)
        {
            var session = (ShikijuSessionObject)HttpContext.Session["ShikijuSessionObject"];
            var table = new Reply();
            table.Delete(id);
            dynamic reply = new ExpandoObject();
            reply.id = id;
            return Json(reply);
        }

        
    }
}
